package Main;

import Student_Course.StudentView;
import Teacher_Course_Student.TeacherView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class UserLogin {

    // 声明静态变量来存储学号或工号
    private static String userId;
    private static int courseId;
    private static String userName;

    public static void login() throws Exception {
        // 1. 加载 JDBC 驱动
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        // 2. 获取数据库连接
        String url = "jdbc:sqlserver://localhost:1433;databaseName=Student_Grade_Management_System;trustServerCertificate=true";
        String user = "ysy";
        String password = "051018";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Scanner scan = new Scanner(System.in);

        try {
            conn = DriverManager.getConnection(url, user, password);

            // 3. 准备 SQL 查询语句
            String sql = "SELECT * FROM 用户 WHERE 账号 = ? AND 密码 = ? AND 身份 = ?";
            ps = conn.prepareStatement(sql);

            // 4. 获取用户输入
            System.out.print("请输入账户：");
            String username = scan.nextLine();
            System.out.print("请输入密码(6位)：");
            String psw = scan.nextLine();
            System.out.print("你的身份（学生/教师）：");
            String ide = scan.nextLine();

            // 5. 设置查询参数
            ps.setString(1, username);  // 设置账号
            ps.setString(2, psw);        // 设置密码
            ps.setString(3, ide);        // 设置身份
            // 6. 执行查询
            rs = ps.executeQuery();

            // 7. 判断登录结果
            if (rs.next()) {
                // 8. 根据身份获取学号或工号
                if ("学生".equals(ide)) {
                    // 通过账号（学生姓名）查询学号
                    String studentSql = "SELECT 学号 FROM 学生 WHERE 学生姓名 = ?";
                    try (PreparedStatement psStudent = conn.prepareStatement(studentSql)) {
                        psStudent.setString(1, username);
                        try (ResultSet studentRs = psStudent.executeQuery()) {
                            if (studentRs.next()) {
                                System.out.println("登录成功!");
                                userId = studentRs.getString("学号");
                                StudentView.student();
                            } else {
                                System.out.println("未找到该学生信息！");
                            }
                        }
                    }
                } else if ("教师".equals(ide)) {
                    // 通过账号（教师姓名）查询工号
                    String teacherSql = "SELECT 工号, 教师姓名 FROM 教师 WHERE 教师姓名 = ?";
                    try (PreparedStatement psTeacher = conn.prepareStatement(teacherSql)) {
                        psTeacher.setString(1, username);
                        try (ResultSet teacherRs = psTeacher.executeQuery()) {
                            if (teacherRs.next()) {
                                System.out.println("登录成功!");

                                userName = teacherRs.getString("教师姓名");
                                String sql2 = "SELECT 课程编号 FROM 学生选课操作界面 WHERE 教师姓名 = ?";
                                try (PreparedStatement psStudent2 = conn.prepareStatement(sql2)) {
                                    psStudent2.setString(1, userName);
                                    try (ResultSet studentRs2 = psStudent2.executeQuery()) {
                                        if (studentRs2.next()) {
                                            courseId = studentRs2.getInt("课程编号");
                                            TeacherView.teacher();
                                        } else {
                                            System.out.println("未找到该教师的课程信息！");
                                        }
                                    }
                                }
                            } else {
                                System.out.println("未找到该教师信息！");
                            }
                        }
                    }
                }

            } else {
                System.out.println("登录失败!");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 9. 关闭资源
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (conn != null) conn.close();
            scan.close();
        }
    }

    // 提供一个静态方法获取学号/工号
    public static String getUserId() {
        return userId;
    }

    public static int getCourseId() {
        return courseId;
    }

    public static String getUsername() {
        return userName;
    }
}
